package com.bci.pwtz.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.Dydlfjx;
import com.bci.pwtz.mysql.model.Dykgg;
import com.bci.pwtz.mysql.model.Dypdx;
import com.bci.pwtz.mysql.model.Dyxl;
import com.bci.pwtz.service.BusinessCommonService;
import com.bci.pwtz.service.DydlfjxService;
import com.bci.pwtz.service.DykggService;
import com.bci.pwtz.service.DypdxService;
import com.bci.pwtz.service.DyxlService;

@Controller
@RequestMapping("/user/dykgg")
public class DykggController extends AbstractController {
	@Autowired
	private DyxlService dyxlService;
	@Autowired
	private DypdxService dypdxService;
	@Autowired
	private DydlfjxService dydlfjxService;
	@Autowired
	private DykggService dykggService;
	@Autowired
    private BusinessCommonService businessCommonService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PaginationList<Dykgg> list(String stationCode, String deviceName,
			int pageSize, int pageNo) throws PwtzException {

		PaginationList<Dykgg> list = new PaginationList<Dykgg>();
		try {
			Long companyid = getLastCompanyId();
			list = dykggService.selectDykggByDyzf(stationCode, deviceName,
					companyid, pageSize, pageNo);
			if (list != null) {
				for (int i = 0; i < list.getRecords().size(); i++) {
					int zftype = list.getRecords().get(i)
							.getStationdevicetype();
					if (zftype == 16) {
						Dypdx dypdx = dypdxService.load(list.getRecords()
								.get(i).getStationcode());
						if (dypdx != null) {
							list.getRecords().get(i).setStationcode(
									dypdx.getDevicename());
							Dyxl dyxl = dyxlService.load(dypdx.getLinecode());
							if (dyxl != null) {
								list.getRecords().get(i).setLinecode(
										dyxl.getDevicename());
							}
						}
					} else if (zftype == 17) {
						Dydlfjx dydlfjx = dydlfjxService.load(list.getRecords()
								.get(i).getStationcode());
						if (dydlfjx != null) {
							list.getRecords().get(i).setStationcode(
									dydlfjx.getDevicename());
							Dyxl dyxl = dyxlService.load(dydlfjx.getLinecode());
							if (dyxl != null) {
								list.getRecords().get(i).setLinecode(
										dyxl.getDevicename());
							}
						}
					}
				}
			}
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping(value = "/edite", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edite(String data)
    {
        Map<String, String> map = new HashMap<String, String>();
        try
        {
            //data=java.net.URLDecoder.decode(data,"UTF-8");
            String [] list=decodeURL(data.split("&"));
            				for(int i=0;i<list.length;i++){
				    String[] array=list[i].split("=");
				    if(array.length==1){
				        map.put(array[0], "");
				    }else{
				        map.put(array[0], array[1]);
				    }
				}
				int retCount = businessCommonService.getCountByDeviceName(getLastCompanyId(),(long)AbstractController.低压开关柜, map.get("devicename"),map.get("deviceid"));
				if(retCount>0){
				    map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "名称已存在！");
				    return map;
				}
				CompanyUser user=(CompanyUser) session.getAttribute("user");
            Dykgg device=new Dykgg();
            device.setDeviceid(map.get("deviceid"));
            device.setDevicename(map.get("devicename"));
            device.setAssetnumber(map.get("assetnumber"));
            device.setAssetsnature(map.get("assetsnature"));
            device.setAssetunit(map.get("assetunit"));
            device.setCommissioningdate(map.get("commissioningdate"));
            device.setCompanyid(user.getLastCompanyId());
            device.setCreateid(user.getCompanyUserId());
            device.setDevicenumber(map.get("devicenumber"));
            device.setEquipmentstate(map.get("equipmentstate"));
            device.setFactorydate(map.get("factorydate"));
            device.setLastoperatorid(user.getCompanyUserId());
            device.setLinecode(map.get("linecode"));
            device.setLocalcity(map.get("localcity"));
            device.setMaintenanceteam(map.get("maintenanceteam"));
            device.setModel(map.get("model"));
            device.setOperationstate(map.get("operationstate"));
            device.setOperationunit(map.get("operationunit"));
            device.setProductionfactory(map.get("productionfactory"));
            device.setSerialnumber(map.get("serialnumber"));
            device.setSyncstatus(0);
            device.setVoltagelevel(map.get("voltagelevel"));
            device.setDepth(PwtzUtils.passDouble(map.get("depth")));
            device.setEquipmentowner(map.get("equipmentowner"));
            device.setGiscode(map.get("giscode"));
            device.setHeight(PwtzUtils.passDouble(map.get("height")));
            device.setIsbackcover(map.get("isbackcover"));
            device.setStationcode(map.get("stationcode"));
            device.setStationdevicetype(PwtzUtils.passInt(map.get("stationdevicetype")));
            device.setType(map.get("type"));
            device.setWidth(PwtzUtils.passDouble(map.get("width")));
            int ret = dykggService.update(device);
            if (ret > 0)
            {
                setLog(device.getDeviceid(), device.getCompanyid(), AbstractController.低压开关柜, AbstractController.update, "修改低压开关柜["+device.getDevicename()+"]成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "编辑低压开关柜成功");
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "编辑低压开关柜失败");
            }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(String data)
    {
        Map<String, String> map = new HashMap<String, String>();
        try
        {
            //data=java.net.URLDecoder.decode(data,"UTF-8");
            String [] list=decodeURL(data.split("&"));
            for(int i=0;i<list.length;i++){
                String[] array=list[i].split("=");
                if(array.length==1){
                    map.put(array[0], "");
                }else{
                    map.put(array[0], array[1]);
                }
            }
            int retCount = businessCommonService.getCountByDeviceName(getLastCompanyId(),(long)AbstractController.低压开关柜, map.get("devicename"));
            if(retCount>0){
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "名称已存在！");
                return map;
            }
            CompanyUser user=(CompanyUser) session.getAttribute("user");
            Dykgg device=new Dykgg();
            UUID uuid=UUID.randomUUID();
            device.setDeviceid(uuid.toString());
            device.setDevicename(map.get("devicename"));
            device.setAssetnumber(map.get("assetnumber"));
            device.setAssetsnature(map.get("assetsnature"));
            device.setAssetunit(map.get("assetunit"));
            device.setCommissioningdate(map.get("commissioningdate"));
            device.setCompanyid(user.getLastCompanyId());
            device.setCreateid(user.getCompanyUserId());
            device.setDevicenumber(map.get("devicenumber"));
            device.setEquipmentstate(map.get("equipmentstate"));
            device.setFactorydate(map.get("factorydate"));
            device.setLastoperatorid(user.getCompanyUserId());
            device.setLinecode(map.get("linecode"));
            device.setLocalcity(map.get("localcity"));
            device.setMaintenanceteam(map.get("maintenanceteam"));
            device.setModel(map.get("model"));
            device.setOperationstate(map.get("operationstate"));
            device.setOperationunit(map.get("operationunit"));
            device.setProductionfactory(map.get("productionfactory"));
            device.setSerialnumber(map.get("serialnumber"));
            device.setSyncstatus(0);
            device.setVoltagelevel(map.get("voltagelevel"));
            device.setDepth(PwtzUtils.passDouble(map.get("depth")));
            device.setEquipmentowner(map.get("equipmentowner"));
            device.setGiscode(map.get("giscode"));
            device.setHeight(PwtzUtils.passDouble(map.get("height")));
            device.setIsbackcover(map.get("isbackcover"));
            device.setStationcode(map.get("stationcode"));
            device.setStationdevicetype(PwtzUtils.passInt(map.get("stationdevicetype")));
            device.setType(map.get("type"));
            device.setWidth(PwtzUtils.passDouble(map.get("width")));
            Dykgg ret = dykggService.add(device);
            if (ret!=null)
            {
                setLog(device.getDeviceid(), device.getCompanyid(), AbstractController.低压开关柜, AbstractController.add, "添加低压开关柜["+device.getDevicename()+"]成功", getUserId());
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "添加低压开关柜成功");
            }
            else
            {
                map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "添加低压开关柜失败");
            }
        }
        catch (PwtzDatabaseException e)
        {
            map = packageException(e);
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> delete(String deviceid){
	    Map<String,String> map=new HashMap<String, String>();
	    int result=0;
	    try
	    {
	        Dykgg device=dykggService.load(deviceid);
	        result=dykggService.delete(deviceid);
	        if (result>0&&device!=null)
	        {
	            setLog(device.getDeviceid(), device.getCompanyid(), AbstractController.低压开关柜, AbstractController.delete, "删除低压开关柜["+device.getDevicename()+"]成功", getUserId());
	            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null, "删除低压开关柜成功");
	        }
	        else
	        {
	            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "删除低压开关柜失败");
	        }
	    }
	    catch (PwtzDatabaseException e)
	    {
	        map = packageException(e);
	    }
	    catch (CanNotDeleteException e)
	    {
	        map = packageException(e);
	    }
	    return map;
	}
}
